//
//  MGHomeViewController.swift
//  微博
//
//  Created by 陈明光 on 2019/11/5.
//  Copyright © 2019 陈明光. All rights reserved.
//

import UIKit
//定义全局常量，尽量使用private
private let cellId = "cellId"


class MGHomeViewController: MGBaseViewController {

    //微博数据数组
    private lazy var statusList = [String]()
    
    //加载数据
    override func loadData() {
        
        //用网络工具加载微博数据
        MGNetworkManager.shared.statusList { (list, isSuccess) in
            print(list as Any)
        }
        print("开始加载数据\(MGNetworkManager.shared)")
        
//        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
//        let params = ["access_token" : "2.00a926kGNUeSHCf02ac3049eZKdVpD"]
//
//        MGNetworkManager.shared.get(urlString, parameters: params, progress:nil , success: { (_, json) in
//            print(json as Any)
//        }) { (_, error) in
//            print("网络请求失败\(error)")
//        }
        //swift5.0封装AFN GET/POST失败
//        MGNetworkManager.shared.request(URLString: urlString, parameters: params as [String : AnyObject]) { (json, isSuccess) in
//            print(json as Any)
//        }
        
        print("开始加载数据\(MGNetworkManager.shared)")
        //模拟‘延时’加载数据 -> dispath_after
        //异步延时
        //由主队列调度从现在开始异步执行时间
        DispatchQueue.main.asyncAfter(deadline: DispatchTime.now() + 1) {
            for i in 0..<30 {
                if self.isPullup{
                    //将数据最加到底部
                    self.statusList.append("上拉\(i)")
                }else{
                    //将数据源插入到数组的顶部
                    self.statusList.insert(i.description, at: 0)
                }
              
            }
            print("刷新表格")
            //结束刷新控件
            self.refreshControl?.endRefreshing()
            //恢复上拉刷新标记
            self.isPullup = false
            
            //刷新表格
            self.tableView?.reloadData()
        }
    }
        
       
    
    //显示好友
    @objc private func showFriends(){
        print(#function)
        
        let vc = MGDemoViewController()
        //push的时候隐藏底部导航栏
        //vc.hidesBottomBarWhenPushed = true
        //push的动作时nav做的
        //push一个界面
        navigationController?.pushViewController(vc, animated: true)
        
    }

}
//MARK: 表格数据源方法 - 具体的数据源方法实现，不需要super
extension MGHomeViewController{
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return  statusList.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        //1.提取cell
        let cell = tableView.dequeueReusableCell(withIdentifier: cellId, for: indexPath)
        
        //2.设置cell
        cell.textLabel?.text = statusList[indexPath.row]
        
        //3.返回cell
        return cell
    }
}

//MARK: - 设置界面
extension MGHomeViewController{
    //重写父类的方法
    override func setupTableView() {
        super.setupTableView()

        //设置导航栏按钮
        navItem.leftBarButtonItem = UIBarButtonItem(title: "好友", target: self, action: #selector(showFriends))
        
        //注册原型cell
        tableView?.register(UITableViewCell.self, forCellReuseIdentifier: cellId)
    }
}
